export function formatTime (data) {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

/**
 * const newDateStr = Utils.parseTime(dataStr, '{y}-{m}-{d} {h}:{i}:{s}')
 * */
export function parseTime(time, cFormat) {
  if (arguments.length === 0) {
    return null
  }
  const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
  let date
  if (typeof time === 'object') {
    date = time
  } else {
    if (('' + time).length === 10) time = parseInt(time) * 1000
    date = new Date(time)
  }
  const formatObj = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    a: date.getDay()
  }
  const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
    let value = formatObj[key]
    if (key === 'a') return ['一', '二', '三', '四', '五', '六', '日'][value - 1]
    if (result.length > 0 && value < 10) {
      value = '0' + value
    }
    return value || 0
  })
  return time_str
} 

// 删除方法
export function remove(array, val) {
  for (var i = 0; i < array.length; i++) {
       if (array[i] == val) {
         array.splice(i, 1);
    }
  }
  return -1;
}
// 删除方法2  
export function remove2(array, index) {
  return array.splice(index, 1);
}

//将价格数据处理 单位/万 或 0
export function fromaterNewNumber(str) {
  let newNumber
  if (str == null) {
    newNumber = 0
  } else {
    newNumber = parseInt(str) / 10000
  }
  return newNumber
}

/**
 *  检查任意数值的
 *  @return digit 最大整位数 1320 => 1000
 *  @return unitName 返回合适的单位  万 
 *  @return divideSum 除以的数目
 *  @return toFixedLength  toFixed 截取位数
 * */
export function checkSumRightUnit(sum) {
  const digit = Math.pow(10, parseInt(sum).toString().length - 1) // 1320 => 1000 最大整位数
  let unitName = ''
  let divideSum = 1
  let toFixedLength = 0
  if (digit >= 100000) {
    unitName = '万'
    divideSum = 10000
    toFixedLength = 2
  }
  const obj = { digit: digit, unitName: unitName, divideSum: divideSum, toFixedLength: toFixedLength }
  // console.log('checkSumUnit()', 'sum', sum, 'digit', digit, '返回单位名称', unitName, '除以的数目', divideSum, 'toFixed', toFixedLength)
  return obj
}

/***
 * @params array 数组
 * @return string 返回字符串
*/
export function arrParamsToString(arr) {
  let now_str = ''
  if (arr.length === 1) {
    now_str = arr[0]
  } else if (arr.length > 1) {
    now_str = arr.join(',')
  }
  return now_str
}

/**
 *  检查任意数值的
 *  @return digit 最大整位数 1320 => 1000
 *  @return unitName 返回合适的单位 个 十 百 千 万 十万 百万
 *  @return divideSum 除以的数目
 *  @return toFixedLength  toFixed 截取位数
 * */
export function checkSumRightUnitWithFixed(sum, toFixed) {
  const digit = Math.pow(10, parseInt(sum).toString().length - 1) // 1320 => 1000 最大整位数
  let unitName = ''
  let divideSum = 1
  let toFixedLength = toFixed ? toFixed : 0
  if (digit >= 100000) {
    unitName = '万'
    divideSum = 10000
    toFixedLength = 0
  }
  const obj = { digit: digit, unitName: unitName, divideSum: divideSum, toFixedLength: toFixedLength }
  console.log('checkSumRightUnitWithFixed()', 'sum', sum, 'digit', digit, '返回单位名称', unitName, '除以的数目', divideSum, 'toFixed', toFixedLength)
  return obj
}